home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / INVENTOR / globe.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.9 KB  |  241 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <math.h>
  11.  
  12. #include <GL/gl.h>
  13. #include <GL/glu.h>
  14. #include <GL/glut.h>
  15.  
  16. #include <Inventor/SoDB.h>
  17. #include <Inventor/nodes/SoComplexity.h>
  18. #include <Inventor/nodes/SoFont.h>
  19. #include <Inventor/nodes/SoGroup.h>
  20. #include <Inventor/nodes/SoSeparator.h>
  21. #include <Inventor/nodes/SoSphere.h>
  22. #include <Inventor/nodes/SoText2.h>
  23. #include <Inventor/nodes/SoTexture2.h>
  24. #include <Inventor/nodes/SoTranslation.h>
  25.  
  26. #include <Inventor/nodes/SoTexture2Transform.h>
  27.  
  28. #include <Inventor/SoInput.h>
  29. #include <Inventor/SbViewportRegion.h>
  30. #include <Inventor/nodes/SoSeparator.h>
  31. #include <Inventor/actions/SoGLRenderAction.h>
  32. #include <Inventor/nodes/SoCylinder.h>
  33. #include <Inventor/nodes/SoDirectionalLight.h>
  34. #include <Inventor/nodes/SoEventCallback.h>
  35. #include <Inventor/nodes/SoMaterial.h>
  36. #include <Inventor/nodes/SoPerspectiveCamera.h>
  37. #include <Inventor/nodes/SoRotationXYZ.h>
  38. #include <Inventor/nodes/SoTransform.h>
  39. #include <Inventor/nodes/SoTranslation.h>
  40.  
  41. /* Some <math.h> files do not define M_PI... */
  42. #ifndef M_PI
  43. #define M_PI 3.14159265358979323846
  44. #endif
  45.  
  46. int W = 300, H = 300;
  47. int spinning = 0;
  48. GLubyte *image = NULL;
  49. SoSeparator *root;
  50. SoRotationXYZ *globeSpin;
  51. float angle = 0.0;
  52. int moving  = 0;
  53. int begin;
  54.  
  55. void
  56. reshape(int w, int h)
  57. {
  58.   glViewport(0, 0, w, h);
  59.   W = w;
  60.   H = h;
  61.   if (image)
  62.     free(image);
  63.   image = (GLubyte *) malloc(W * H * 3);
  64. }
  65.  
  66. void
  67. renderScene(void)
  68. {
  69.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  70.   SbViewportRegion myViewport(W, H);
  71.   SoGLRenderAction myRenderAction(myViewport);
  72.   myRenderAction.apply(root);
  73. }
  74.  
  75. void
  76. redraw(void)
  77. {
  78.   renderScene();
  79.   glutSwapBuffers();
  80. }
  81.  
  82. void
  83. globeScene(void)
  84. {
  85.    root = new SoSeparator;
  86.    root->ref();
  87.  
  88.    // Add a camera and light
  89.    SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
  90.    myCamera->position.setValue(0., 0., 2.2);
  91.    myCamera->heightAngle = M_PI/2.5; 
  92.    myCamera->nearDistance = 0.5;
  93.    myCamera->farDistance = 10.0;
  94.    root->addChild(myCamera);
  95.    root->addChild(new SoDirectionalLight);
  96.  
  97.    SoRotationXYZ *globalRotXYZ = new SoRotationXYZ;
  98.    globalRotXYZ->axis = SoRotationXYZ::X;
  99.    globalRotXYZ->angle = M_PI/9;
  100.    root->addChild(globalRotXYZ);
  101.  
  102.    // Set up the globe transformations
  103.    globeSpin = new SoRotationXYZ;
  104.    root->addChild(globeSpin);
  105.    globeSpin->angle = angle;
  106.    globeSpin->axis = SoRotationXYZ::Y;  // rotate about Y axis
  107.  
  108.    // Add the globe, a sphere with a texture map.
  109.    // Put it within a separator.
  110.    SoSeparator *sphereSep = new SoSeparator;
  111.    SoTexture2  *myTexture2 = new SoTexture2;
  112.    SoComplexity *sphereComplexity = new SoComplexity;
  113.    sphereComplexity->value = 0.55;
  114.    root->addChild(sphereSep);
  115.    sphereSep->addChild(myTexture2);
  116.    sphereSep->addChild(sphereComplexity);
  117.    sphereSep->addChild(new SoSphere);
  118.    myTexture2->filename = "globe.rgb";
  119. }
  120.  
  121. void
  122. updateModels(void)
  123. {
  124.   globeSpin->angle = angle;
  125.   glutPostRedisplay();
  126. }
  127.  
  128. void
  129. animate(void)
  130. {
  131.   angle += 0.1;
  132.   updateModels();
  133. }
  134.  
  135. void
  136. setAnimation(int enable)
  137. {
  138.   if(enable) {
  139.     spinning = 1;
  140.     glutIdleFunc(animate);
  141.   } else {
  142.     spinning = 0;
  143.     glutIdleFunc(NULL);
  144.     glutPostRedisplay();
  145.   }
  146. }
  147.  
  148. /* ARGSUSED */
  149. void
  150. keyboard(unsigned char ch, int x, int y)
  151. {
  152.   if(ch == ' ') {
  153.     setAnimation(0);
  154.     animate();
  155.   }
  156. }
  157.  
  158. void
  159. menuSelect(int item)
  160. {
  161.    switch(item) {
  162.    case 1:
  163.      animate();
  164.      break;
  165.    case 2:
  166.       if(!spinning) {
  167.             setAnimation(1);
  168.        } else {
  169.             setAnimation(0);
  170.        }
  171.       break;
  172.    }
  173. }
  174.  
  175. void
  176. vis(int visible)
  177. {
  178.   if (visible == GLUT_VISIBLE) {
  179.     if (spinning)
  180.       glutIdleFunc(animate);
  181.   } else {
  182.     if (spinning)
  183.       glutIdleFunc(NULL);
  184.   }
  185. }
  186.  
  187. /* ARGSUSED */
  188. void
  189. mouse(int button, int state, int x, int y)
  190. {
  191.   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  192.     setAnimation(0);
  193.     moving = 1;
  194.     begin = x;
  195.   }
  196.   if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  197.     moving = 0;
  198.     glutPostRedisplay();
  199.   }
  200. }
  201.  
  202. /* ARGSUSED */
  203. void
  204. motion(int x, int y)
  205. {
  206.   if (moving) {
  207.     angle = angle + .01 * (x - begin);
  208.     begin = x;
  209.     updateModels();
  210.   }
  211. }
  212.  
  213. int
  214. main(int argc, char **argv)
  215. {
  216.   glutInit(&argc, argv);
  217.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  218.  
  219.   SoDB::init();
  220.   globeScene();
  221.  
  222.   glutInitWindowSize(W, H);
  223.   glutCreateWindow("As the world turns");
  224.   glutDisplayFunc(redraw);
  225.   glutReshapeFunc(reshape);
  226.   glutCreateMenu(menuSelect);
  227.   glutAddMenuEntry("Step", 1);
  228.   glutAddMenuEntry("Toggle spinning", 2);
  229.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  230.   glutKeyboardFunc(keyboard);
  231.   glutMouseFunc(mouse);
  232.   glutMotionFunc(motion);
  233.   glutVisibilityFunc(vis);
  234.  
  235.   /* Enable depth testing for Open Inventor. */
  236.   glEnable(GL_DEPTH_TEST);
  237.  
  238.   glutMainLoop();
  239.   return 0;             /* ANSI C requires main to return int. */
  240. }
  241.